/**
 * 简化的路由工具
 * 用于获取当前页面路径等信息
 */

interface RouteInfo {
  path: string
  query: Record<string, any>
}

/**
 * 获取当前页面路由信息
 * @returns 当前路由信息
 */
export function useRoute(): RouteInfo {
  // 获取当前页面实例
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]

  if (!currentPage) {
    return { path: '/pages/index/index', query: {} }
  }

  // 从页面路径获取路由信息
  const route = currentPage.route || ''
  const options = currentPage.options || {}

  return {
    path: `/${route}`,
    query: options
  }
}

/**
 * 路由跳转
 * @param url 目标路径
 * @param type 跳转类型：navigateTo、redirectTo、switchTab、reLaunch
 */
export function useRouter() {
  const navigateTo = (url: string) => {
    uni.navigateTo({ url })
  }

  const redirectTo = (url: string) => {
    uni.redirectTo({ url })
  }

  const switchTab = (url: string) => {
    uni.switchTab({ url })
  }

  const reLaunch = (url: string) => {
    uni.reLaunch({ url })
  }

  const navigateBack = (delta = 1) => {
    uni.navigateBack({ delta })
  }

  return {
    navigateTo,
    redirectTo,
    switchTab,
    reLaunch,
    navigateBack
  }
}

export default {
  useRoute,
  useRouter
}